package com.lagou.client;

import com.lagou.model.*;

import java.io.*;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

/**
 * 编程实现客户端的主界面和相应功能的实现
 */

public class ClientView {

    //为了可以使用输入输出流采用合成复用原则
    private ClientInitClose cic;
    //4.ObjectOutputStream 和ObjectInputStream类 并将数据写入文件中
    ObjectOutputStream oos2 = null;
    ObjectInputStream ois2 = null;

    //通过构造方法实现成员变量的初始化
    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }


    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        oos2 = cic.getOos();
        ois2 = cic.getOis();
        while (true) {
            System.out.println("   \n\n\t\t在线考试系统");
            System.out.println("-------------------------------------------------------");
            System.out.print("   [1] 学员系统");
            System.out.println("      [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("-------------------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("正在进入学员系统");
                    studentLogin();
                    break;
                case 2:
                    System.out.println("正在进入管理员系统");
                    clientManagerLogin();
                    break;
                case 0:
                    System.out.println("正在退出系统...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！！！");
            }
        }

    }

    /**
     * 自定义成员方法，实现管理员登陆功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        boolean flag = true;
        //1.准备管理员登陆的相关信息
        System.out.println("请输入管理员的账户信息:");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck",new User(userName,password));
        //2.将UserMessage对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功");
        //3.接收服务器的处理结果并给出提示
        tum = (UserMessage)cic.getOis().readObject();
        if("success".equals(tum.getType())){
            System.out.println("登陆成功，欢迎使用");
            while (flag) {
                System.out.println("1.学员管理模块");
                System.out.println("2.考题管理模块");
                System.out.println("0.退出");
                int index = ClientScanner.getScanner().nextInt();
                switch (index){
                    case 1:
                        studentManagerLogin();
                        break;
                    case 2:
                        ExamManager();
                        break;
                    case 0:
                        flag = false;
                        break;
                    default:
                        System.out.println("输入错误，请重新选择！！！");
                }
            }
        }else {
            System.out.println("用户名或密码错误");
        }

    }/**
     * 自定义成员方法，实现学生管理系统功能
     */
    //创建List集合用于存储学员账号信息和密码
    private void studentManagerLogin() throws IOException, ClassNotFoundException {
        boolean flag = true;

        List<User> student = new LinkedList<>();
        User user = null;
        while (flag) {
            //4.在启动信息管理系统时，将文件中的数据传入集合中
            try {
                //4.1 关联文件
                ois2 = new ObjectInputStream(new FileInputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\data.txt"));
                //4.2将data中的学员数据导入到List集合中
                Object obj = ois2.readObject();
                student = (List<User>) obj;

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    ois2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //2.让用户选择需要进行什么操作
            //1 增加 2 删除 3 修改 4 查看 5 遍历学生信息
            System.out.println("请选择需要执行的操作：");
            System.out.println("1.增加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.查看学生信息");
            //System.out.println("5.登陆学生账号");
            System.out.println("0.关闭信息管理系统");
            int index = ClientScanner.getScanner().nextInt();
            switch (index){
                //2.1增加功能
                case 1:
                    int num = 0;
                    //1.准备学员登陆的相关信息
                    System.out.println("请输入学员的账户信息:");
                    String userName = ClientScanner.getScanner().next();
                    System.out.println("请输入学员的密码信息：");
                    String password = ClientScanner.getScanner().next();
                    int len = student.size();
                    user = new User(userName,password);
                    student.add(len,user);
                    //修改过后保存数据到文件
                    try {
                        //与文件相关联
                        oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\data.txt"));
                        //将整个LinkedList类型写入输出流
                        oos2.writeObject(student);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;

                //2.2 删除功能
                case 2:
                    System.out.println("请输入要删除的学生的账户：");
                    userName = ClientScanner.getScanner().next();
                    int number = 0;
                    for(User u:student){
                        if (u.getUserName().equals(userName)){
                            break;
                        }
                        number++;
                    }
                    student.remove(number);
                    //修改过后保存数据到文件
                    try {
                        //与文件相关联
                        oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\data.txt"));
                        //将整个LinkedList类型写入输出流
                        oos2.writeObject(student);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;

                //2.3 修改功能
                case 3:
                    System.out.println("请输入要修改的学生的账户：");
                    userName = ClientScanner.getScanner().next();
                    number = 0;
                    for(User u:student){
                        if (u.getUserName().equals(userName)){
                            break;
                        }
                        number++;
                    }
                    System.out.println("请输入学员的账户信息:");
                    userName = ClientScanner.getScanner().next();
                    System.out.println("请输入学员的密码信息：");
                    password = ClientScanner.getScanner().next();
                    user = new User(userName,password);
                    len = student.size();
                    student.set(number,user);
                    //修改过后保存数据到文件
                    try {
                        //与文件相关联
                        oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\data.txt"));
                        //将整个LinkedList类型写入输出流
                        oos2.writeObject(student);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;

                //2.4 查看功能
                case 4:
                    System.out.println("请输入要查看的学生的账户信息：");
                    userName = ClientScanner.getScanner().next();
                    number = 0;
                    for(User u:student){
                        if (u.getUserName().equals(userName)){
                            break;
                        }
                        number++;
                    }
                    user = student.get(number);
                    System.out.println("账户：" + user.getUserName() + "密码：" + user.getPassword());
                    break;

                    //2.5 退出信息管理系统
                case 0:
                    try {
                        flag = false;
                        //与文件相关联
                        oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\data.txt"));
                        //将整个LinkedList类型写入输出流
                        oos2.writeObject(student);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        //关闭流
                        if (oos2 != null){
                            try {
                                oos2.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
                default:
                    System.out.println("输入错误，请重新选择！！！");
            }
        }


    }

    /**
     * 自定义成员方法，实现学员用户功能
     */
    private void studentLogin() throws IOException, ClassNotFoundException {
        boolean flag = true;
        List<User> student = new LinkedList<>();
        User user = null;
        //4.在启动信息管理系统时，将文件中的数据传入集合中
        try {
            //4.1 关联文件
            ois2 = new ObjectInputStream(new FileInputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\data.txt"));
            //4.2将data中的学员数据导入到List集合中
            Object obj = ois2.readObject();
            student = (List<User>) obj;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                ois2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        while (flag) {
            //学员所能执行的操作
            System.out.println("-----------------------学员登陆-------------------------");
            System.out.println("请选择需要执行的操作：");
            System.out.println("1.登陆");
            System.out.println("2.修改密码");
            System.out.println("0.退出");
            int index = ClientScanner.getScanner().nextInt();
            switch (index){
                //登陆
                case 1:
                    System.out.println("请输入学员的账户信息:");
                    String userName = ClientScanner.getScanner().next();
                    System.out.println("请输入学员的密码信息：");
                    String password = ClientScanner.getScanner().next();
                    user = new User(userName,password);
                    UserMessage tum = new UserMessage("userCheck", user, student);
                    //2.将UserMessage对象通过对象输出流发送给服务器
                    cic.getOos().writeObject(tum);
                    System.out.println("学员客户端发送管理员账户信息成功");
                    //3.接收服务器的处理结果并给出提示
                    tum = (UserMessage)cic.getOis().readObject();
                    if("success".equals(tum.getType())){
                        System.out.println("登陆成功，欢迎使用");
                        boolean flag2 = true;
                        List<ExamMessage> examMessageList = new LinkedList<>();
                        List<StudentGrade> studentGrades = new LinkedList<>();
                        //4.1 关联文件
                        ois2 = new ObjectInputStream(new FileInputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\exam.txt"));
                        //4.2将data中的题目数据导入到List集合中
                        Object obj = ois2.readObject();
                        List<Exam>examList = (List<Exam>) obj;
                        while (flag2) {
                            System.out.println("1.开始考试");
                            System.out.println("2.成绩查询");
                            System.out.println("3.导出成绩");
                            System.out.println("0.退出");
                            index = ClientScanner.getScanner().nextInt();
                            switch (index){
                                //开始考试
                                case 1:
                                    for(Exam e : examList ){
                                        System.out.println("编号:" + e.getExamId() + "题目：" + e.getExamQuestion());
                                        System.out.println("请输入答案");
                                        String answer = ClientScanner.getScanner().next();
                                        int len = examMessageList.size();
                                        ExamMessage examMessage = new ExamMessage(e,answer);
                                        examMessageList.add(len, examMessage);
                                    }
                                    int len = studentGrades.size();
                                    StudentGrade studentGrade = new StudentGrade(user,examMessageList);
                                    studentGrades.add(len,studentGrade);
                                    break;
                                //查询分数
                                case 2:
                                    for (StudentGrade sg : studentGrades){
                                        if(user.getUserName().equals(sg.getUser().getUserName())){
                                            int grade = ExamCheck(sg.getExamMessageList());
                                            sg.setGrade(grade);
                                            System.out.println("分数为：" + sg.getGrade());
                                        }
                                    }
                                    break;
                                //导出成绩
                                case 3:
                                    //与文件相关联
                                    oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\studentGrade.txt"));
                                    //将整个LinkedList类型写入输出流
                                    oos2.writeObject(studentGrades);
                                    System.out.println("数据保存成功！！！");
                                    break;
                                case 0:
                                    flag2 = false;
                                    break;
                                default:
                                    System.out.println("输入错误，请重新选择！！！");
                            }
                        }
                    }else {
                        System.out.println("用户名或密码错误");
                    }
                    break;
                //修改密码
                case 2:
                    System.out.println("请输入要修改的学生的账户：");
                    userName = ClientScanner.getScanner().next();
                    int number = 0;
                    for(User u:student){
                        if (u.getUserName().equals(userName)){
                            break;
                        }
                        number++;
                    }
                    System.out.println("请输入学员的密码信息：");
                    password = ClientScanner.getScanner().next();
                    user = new User(userName,password);
                    student.set(number,user);
                    //修改过后保存数据到文件
                    try {
                        flag = false;
                        //与文件相关联
                        oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\data.txt"));
                        //将整个LinkedList类型写入输出流
                        oos2.writeObject(student);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                //退出
                case 0:
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误，请重新选择！！！");
            }

        }

    }

    /**
     * 判断题目答案是否正确以及成绩
     * @param examMessageList
     * @return
     */
    private int ExamCheck(List<ExamMessage> examMessageList){
        int grade = 0;
        for(ExamMessage em : examMessageList){
            if (em.getExam().getExamQuestion().split("\\?")[0].equals(em.getAnswer())) {
                grade ++;
            }
        }
        return grade;
    }

    /**
     * 考试题管理
     * ：增加考题、删除考题、修改考题、查找考题,导入考题
     */
    private void ExamManager() {
        boolean flag = true;

        List<Exam> examList = new LinkedList<>();
        Exam exam = null;
        //4.在启动信息管理系统时，将文件中的数据传入集合中
        try {
            //4.1 关联文件
            ois2 = new ObjectInputStream(new FileInputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\exam.txt"));
            //4.2将data中的学员数据导入到List集合中
            Object obj = ois2.readObject();
            examList = (List<Exam>) obj;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                ois2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        while (flag) {
            //2.让用户选择需要进行什么操作
            //1 增加 2 删除 3 修改 4 查看
            System.out.println("请选择需要执行的操作：");
            System.out.println("1.增加考题");
            System.out.println("2.删除考题");
            System.out.println("3.修改考题");
            System.out.println("4.查看考题");
            System.out.println("0.关闭");
            int index = ClientScanner.getScanner().nextInt();
            switch (index) {
                //2.1增加功能
                case 1:
                    int num = 0;
                    //1.增加考题的信息
                    System.out.println("请输入考题号:");
                    int examId = ClientScanner.getScanner().nextInt();
                    System.out.println("请输入考题内容：");
                    String examQuestion = ClientScanner.getScanner().next();
                    int len = examList.size();
                    exam = new Exam(examId, examQuestion);
                    examList.add(len, exam);
                    break;

                //2.2 删除功能
                case 2:
                    System.out.println("请输入要删除的考题号：");
                    examId = ClientScanner.getScanner().nextInt();
                    int number = 0;
                    for (Exam e : examList) {
                        if (e.getExamId() == examId) {
                            break;
                        }
                        number++;
                    }
                    examList.remove(number);
                    break;

                //2.3 修改功能
                case 3:
                    System.out.println("请输入要修改的考题号：");
                    examId = ClientScanner.getScanner().nextInt();
                    number = 0;
                    for (Exam e : examList) {
                        if (e.getExamId() == examId) {
                            break;
                        }
                        number++;
                    }
                    System.out.println("请输入考题号:");
                    examId = ClientScanner.getScanner().nextInt();
                    System.out.println("请输入考题内容：");
                    examQuestion = ClientScanner.getScanner().next();
                    exam = new Exam(examId,examQuestion);
                    examList.set(number, exam);
                    //修改过后，保存数据到文件中
                    try {
                        flag = false;
                        //与文件相关联
                        oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\exam.txt"));
                        //将整个LinkedList类型写入输出流
                        oos2.writeObject(examList);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;

                //2.4 查看功能
                case 4:
                    System.out.println("请输入要查看的考题号：");
                    examId = ClientScanner.getScanner().nextInt();
                    number = 0;
                    for (Exam e : examList) {
                        if (e.getExamId() == examId) {
                            break;
                        }
                        number++;
                    }
                    exam = examList.get(number);
                    System.out.println("题号：" + exam.getExamId() + "内容：" + exam.getExamQuestion());
                    break;

                //2.5 退出管理系统
                case 0:
                    try {
                        flag = false;
                        //与文件相关联
                        oos2 = new ObjectOutputStream(new FileOutputStream("E:\\拉钩教育\\第一阶段\\the-first-stage\\模块五homework\\ExamSystemClient\\src\\exam.txt"));
                        //将整个LinkedList类型写入输出流
                        oos2.writeObject(examList);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        //关闭流
                        if (oos2 != null) {
                            try {
                                oos2.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
                default:
                    System.out.println("输入错误，请重新选择！！！");
            }
        }
    }
}
